package com.hualan.service.c;

import com.alibaba.fastjson.JSON;
import com.hualan.entity.a.XiaoQu;
import com.hualan.entity.c.YiZhan;
import com.hualan.utils.EntityFactory;
import com.hualan.utils.ExecUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = {"/yizhan","/add","/update"})
public class YiZhanService extends HttpServlet {
    private List<YiZhan> yiZhanList;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询
        this.yiZhanList = new ArrayList<>();
        String sql = "selest * from yizhan";
        Connection conn = (Connection) req.getAttribute("connection");
        try (Statement exec = conn.createStatement();
             ResultSet datas = exec.executeQuery(sql);) {
            while (datas.next()) {
                EntityFactory entityFactory = new EntityFactory(YiZhan.class);
                YiZhan yiZhan = (YiZhan) entityFactory.startCombine(datas);
                this.yiZhanList.add(yiZhan);
            }
            resp.getWriter().write(JSON.toJSONString(this.yiZhanList));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //添加
        if(req.getServletPath().equals("/add")) {
            String sql = "insert into yizhan (courierStationId,courierStationName,courierStationImage,courierStationProvince,courierStationCity,courierStationDistrict,courierStationAddress,courierStationCommunityName,courierStationOrderCount,addTime,courierStationStatus,courierStationSort) values (?,?,?,?,?,?,?,?,?,?,?,?)";
            List<Object> params = new ArrayList<>();
            req.setCharacterEncoding("utf-8");
            params.add(req.getParameter("courierStationId"));
            params.add(req.getParameter("courierStationName"));
            params.add(req.getParameter("courierStationImage"));
            params.add(req.getParameter("courierStationProvince"));
            params.add(req.getParameter("courierStationCity"));
            params.add(req.getParameter("courierStationDistrict"));
            params.add(req.getParameter("courierStationAddress"));
            params.add(req.getParameter("courierStationCommunityName"));
            params.add(req.getParameter("courierStationOrderCount"));
            params.add(req.getParameter("addTime"));
            params.add(req.getParameter("courierStationStatus"));
            params.add(req.getParameter("courierStationSort"));
            ExecUtil.execute(sql, params, req, true);
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);//好
            }
        } else if (req.getServletPath().equals("/update")) {
            String sql = "update yizhan set courierStationProvince=?,courierStationName=? where courierStationId=？";
            
        }

    }
}
